package com.longrise.android.byjk.plugins.tabfirst.answerQuestion;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.NetUtil;


/**
 * Created by sujizhong on 16/10/17.
 * 回答
 */
public class AnswerActivity extends BaseActivity implements View.OnClickListener{

    public static final int RESULT_CODE = 12;

    private Toolbar mTitle;

    private EditText mEdit1;
    private TextView mTextCommit;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_answer;
    }

    @Override
    public void initView() {
        mEdit1 = (EditText) findViewById(R.id.answeracitivity_edit1);
        mTextCommit = (TextView) findViewById(R.id.answeracitivity_textcommit);

        initTitle();
        regEvent();

    }

    private void initTitle(){
        mTitle = (Toolbar) findViewById(R.id.byjk_toolbar);
        mTitle.setTitle(getString(R.string.answeractivity_hd));
        setSupportActionBar(mTitle);
        mTitle.setNavigationOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void regEvent() {
        mEdit1.addTextChangedListener(mTextWatcher);
        mTextCommit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.answeracitivity_textcommit:
                if (checkNetwork()) {
                    commitAnswer();
                }
                break;
        }
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            try {
                String msg1 = mEdit1.getText().toString().trim();
                if (msg1.length() <= 0) {
                    switchClickAble(false, R.drawable.shape_hui);
                } else {
                    switchClickAble(true, R.drawable.selecter_yellowbtn);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private void switchClickAble(boolean bool, int drawable) {
        mTextCommit.setEnabled(bool);
        mTextCommit.setClickable(bool);
        mTextCommit.setBackgroundResource(drawable);
    }

    private void commitAnswer() {
        String msg = mEdit1.getText().toString().trim();
        if (TextUtils.isEmpty(msg)) {
            DZZWTools.showToast(this, getString(R.string.answeracitiv_hint), Toast.LENGTH_SHORT);
            return;

        }
        setResult(AnswerActivity.RESULT_CODE, new Intent().putExtra("answeractivity_content", msg));
        finish();
    }

    private boolean checkNetwork() {
        if (!NetUtil.checkNetEnable()) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
